#coding=utf-8
# @Time :2021/3/28 9:59 下午
# @Author : Jelly
# @File : __init__.py.py
# @Software : PyCharm

"""
1.
用生成器实现，读取一个文件，文件比较大（1T,2T），
假设每次最多只能读取200个字符（可以自己设定一下大小），超过就会崩溃,用yield
文件会有一个特点：差不多150-180个字符，会有一个相同的字母出现，
xxaaaa
...
bbbb
"teemo"
xxaaaabbb
...
bbbbbsss
"teemo"

xxaaaa
...
bbbb
"teemo"


2.优化打 boss：1）英雄死后，需要移除hero_list  2）英雄 和 boss不允许出现血量和负数的情况，归零后即死亡，伤害超出血量时，把血量归零
"""

import random


class Boss:

    def __init__(self, name, blood, attack_num, defense_num):
        self.energy = 0  # 初始化能量为0
        self.name = name  # 名称
        self.blood = blood  # 血量
        self.attack_num = attack_num  # 攻击力
        self.defense_num = defense_num  # 防御力
        self.defense_status = False  # 防御状态

    def attack(self, hero):
        """
        普通攻击
        :param hero:
        :return:
        """
        if hero.defense_status:
            print(f"hero({hero.name})进入了防御状态，攻击miss，造成伤害0，hero剩余血量为 {hero.blood}")
            return
        hurt = self.attack_num - hero.defense_num  # 伤害 = 自身攻击力 - Hero防御力
        if hurt > 0:
            hero.blood -= hurt  # 英雄血量-伤害
            print(f"boss({self.name}) 对 hero({hero.name}) 造成了 {hurt} 点伤害, hero剩余血量为 {hero.blood}")
            self.energy += 1

    def attack_all(self, hero_list):
        """
        群攻
        :param hero_list:
        :return:
        """
        for hero in hero_list:
            if hero.defense_status:
                print(f"hero({hero.name})进入了防御状态，攻击miss，造成伤害0，hero剩余血量为 {hero.blood}")
                continue
            hurt = self.attack_num * 0.8 - hero.defense_num  # 伤害 = 自身攻击力*0.8 - Hero防御力
            if hurt > 0:
                hero.blood -= hurt  # 英雄血量-伤害
                print(f"boss({self.name}) 对 hero({hero.name}) 造成了 {hurt} 点伤害, , hero剩余血量为 {hero.blood}")
        self.energy = 0  # 能量条设置为0

    def defense(self):
        """
        防御
        :return:
        """
        self.defense_status = True
        print(f"boss({self.name}) 进入防御状态!免伤")

    def action(self, hero_list):
        """
        boss每回合干什么事
        :param hero_list:
        :return:
        """
        if random.randint(1, 4) == 1:
            self.defense()  # 进入防御状态
        else:
            index = random.randint(0, len(hero_list) - 1)  # 随机英雄下标
            if self.energy == 3:  # 判断是发动普通攻击还是群攻
                self.attack_all(hero_list)
            else:
                self.attack(hero_list[index])


class Hero:

    def __init__(self, name, blood, attack_num, defense_num):
        self.name = name  # 名称
        self.blood = blood  # 血量
        self.attack_num = attack_num  # 攻击力
        self.defense_num = defense_num  # 防御力
        self.energy = 0  # 初始化能量为0
        self.defense_status = False

    def attack(self, boss):
        """
        普通攻击
        :param boss:
        :return:
        """
        if boss.defense_status:
            print(f"boss({boss.name})进入了防御状态，攻击miss，造成伤害0，boss剩余血量为 {boss.blood}")
            return
        hurt = self.attack_num - boss.defense_num  # 伤害 = 自身攻击力 - boss防御力
        if hurt > 0:
            boss.blood -= hurt  # boss血量-伤害
            print(f"hero({self.name}) 对 boss({boss.name}) 造成了 {hurt} 点伤害, boss剩余血量为 {boss.blood}")
            self.energy += 1

    def super_attack(self, boss):
        """
        必杀技
        :param boss:
        :return:
        """
        if boss.defense_status:
            print(f"boss({boss.name})进入了防御状态，攻击miss，造成伤害0，boss剩余血量为 {boss.blood}")
            return
        hurt = self.attack_num * 2 - boss.defense_num  # 伤害 = 自身攻击力*2 - boss防御力
        if hurt > 0:
            boss.blood -= hurt  # boss血量-伤害
            print(f"hero({self.name}) 对 boss({boss.name}) 造成了 {hurt} 点伤害, boss剩余血量为 {boss.blood}")
        self.energy = 0  # 能量条设置为0

    def defense(self):
        self.defense_status = True
        print(f"hero({self.name}) 进入防御状态!免伤")

    def action(self, boss):
        """
        英雄每回合干什么事
        :param boss:
        :return:
        """
        if random.randint(1, 4) == 1:
            self.defense()  # 进入防御状态
        else:
            if self.energy == 2:  # 判断是发动普通攻击还是群攻
                self.super_attack(boss)
            else:
                self.attack(boss)


def main(boss, hero_list):
    """
    定义回合次数
    :param boss:
    :param hero_list:
    :return:
    """
    import time
    while True:
        if boss.blood <= 0:
            print("boss 阵亡， game over")
            break
        boss.action(hero_list)
        for hero in hero_list:
            hero.action(boss)
        time.sleep(1)
        # 解除防御状态
        boss.defense_status = False
        for hero in hero_list:
            hero.defense_status = False


if __name__ == '__main__':
    boss = Boss('哥斯拉', 1000, 400, 300)
    hero_1 = Hero('奥特曼', 3000, 500, 200)
    hero_2 = Hero('金刚', 5000, 350, 350)
    hero_list = [hero_1, hero_2]
    main(boss, hero_list)